var express = require('express');
var router = express.Router();
var orderModel = require('../../models/order/order_list');
var processModel = require('../../models/order/order_process');


//定义路由，当访问该路由的时候，去数据库查找信息并返回
router.post('/', function (req, res, next) {
    var params = req.body
    let cases = params.cases
    let newState = -1
    switch (cases) {
        case 0:
            newState = 3
            processModel.updateOne({ order_id: params._id }, { $set: { cancel_time: new Date() } }, (err => console.log(err)))
            break;
        case 1:
            newState = 2
            processModel.updateOne({ order_id: params._id }, { $set: { finish_time: new Date() } }, (err => console.log(err)))
            break;
        case 2:
        case 3:
            newState = 0
            processModel.updateOne({ order_id: params._id }, { $set: { take_time: null, finish_time: null, cancel_time: null } }, (err => console.log(err)))
            orderModel.updateOne({ _id: params._id }, { $set: { taker: null } }, (err => console.log(err)))
            break;
    }
    if (cases == 2) orderModel.updateOne({ _id: params._id }, { $set: { publish_time: new Date() } }, (err => console.log(err)))
    orderModel.updateOne({ _id: params._id }, {
        $set: {
            state: newState
        }
    }, (err) => {
        if (!err) {
            res.json({ msg: '订单状态已更新', code: 200 })
        }
    })
})

module.exports = router;
